package com.dobbinsoft.iotechn.user.service.biz;

import com.alibaba.fastjson.JSONObject;
import com.dobbinsoft.iotechn.core.constant.Const;
import com.dobbinsoft.iotechn.core.exception.ServiceException;
import com.dobbinsoft.iotechn.core.model.DeveloperSession;
import com.dobbinsoft.iotechn.user.api.biz.DeveloperBizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * Description:
 * User: rize
 * Date: 2020/6/7
 * Time: 20:51
 */
@Service("developerBizService")
public class DeveloperBizServiceImpl implements DeveloperBizService {

    @Autowired
    private StringRedisTemplate userRedisTemplate;

    @Override
    public DeveloperSession getDeveloperSessionByAccessToken(String accessToken) throws ServiceException {
        String userJson = userRedisTemplate.opsForValue().get(Const.DEVELOPER_REDIS_PREFIX + accessToken);
        if (!StringUtils.isEmpty(userJson)) {
            DeveloperSession developerSession = JSONObject.parseObject(userJson, DeveloperSession.class);
            userRedisTemplate.expire(Const.DEVELOPER_REDIS_PREFIX + accessToken, Const.DEVELOPER_SESSION_EXPIRE_MINUTES, TimeUnit.MINUTES);
            return developerSession;
        }
        return null;
    }

}
